import sys,subprocess
import requestAI,config


'''
打印帮助日志
'''
def printHelpLog():
    print("easy: versio 1.1")

'''
@author niann
@description 获取用户输入信息
'''
def get_user_input():
    parameters = sys.argv[1:]
    if len(parameters) == 0:
        printHelpLog()
        return
    #设置配置文件get_user_input
    if parameters[0] == 'config' :
        if len(parameters) <2:
            print("The command is missing an argument: easy config [your key]")
            return
        config.set_key(parameters[1])
        return
    cmd =  " ".join(parameters) #.join 将空格作为分隔符把列表转为字符串
    return cmd

    
'''
author niann
description 执行用户输入语句并返回结果
'''
def execute(user_input):
    result = subprocess.run(user_input, shell=True, capture_output=True, text=True)
    if result.returncode:
        return result.stderr,1 #发生错误返回1
    return result.stdout,0


"""
@author niann
主程序运行
"""
def main():
    user_input = get_user_input()
    if user_input == None:
        return
    if requestAI.get_key() == None:
        return
    out_info = execute(user_input) # 标准输出
    if out_info[1]:
        err_text = out_info[0]
        print(requestAI.help(err_text))
    else:
        print(out_info[0])


if __name__ == '__main__':
    main()

    
